package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.VisitLog;

/**
 * @desc   访问日志操作类
 * @author ningqian
 * @date 2017-4-11
 */
@Service
public class VisitLogService extends BaseDao<VisitLog> {
	/**
	 * 按条件查询
	 * @param Kindergarten
	 * @return
	 */
	public List<VisitLog> query(VisitLog visitLog, String... date) {
		if(null == visitLog) return super.findAll();
		if(visitLog.getiDisplayLength() > 0){
		  return super.find(createFilter(visitLog, date),visitLog.getiDisplayStart(),visitLog.getiDisplayLength());
		}
		return super.find(createFilter(visitLog, date));
	}

	public Long count(VisitLog visitLog, String... date) {
		return super.count(createFilter(visitLog, date));
	}
	
	/**
	 * 拼装条件(直播id、创建时间)
	 * @param Kindergarten
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(VisitLog visitLog, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != visitLog.getLiveId()){
		  dc.add(Restrictions.eq("liveId", visitLog.getLiveId()));
	  }
	  
	  if(StringUtil.isNotEmpty(visitLog.getUserNickName())){
		  dc.add(Restrictions.eq("userNickName", visitLog.getUserNickName()));
	  }
	  
	  if(StringUtil.isNotEmpty(visitLog.getStatus())){
		  dc.add(Restrictions.eq("status", visitLog.getStatus()));
	  }
	  
	  if(StringUtil.isNotEmpty(visitLog.getOperateDesc())){
		  dc.add(Restrictions.like("operateDesc", visitLog.getOperateDesc(),MatchMode.ANYWHERE));
	  }
	  
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}
	
}
